package vy.org.app.page.member;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.dao.ViewVyMemberDO;
import org.vy.dal.dao.ViewVyMemberSubscriptionDO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDAO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDO;
import org.vy.dal.dao.member.SearchMemberDAO;
import org.vy.dal.dao.member.SearchMemberSubscriptionDAO;

import vy.org.app.common.AppConstants;
import vy.org.app.common.BaseAction;
import vy.org.app.common.ModeEnum;
import vy.org.app.common.ModuleEnum;

public class MntnMemberAction extends BaseAction {

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		
		MntnMemberForm form = (MntnMemberForm)actionForm;
		try{
			List<ViewVyLookupCitiesDO> countryLst = ViewVyLookupCitiesDAO.findUniqueCountries();
			List<ViewVyLookupCitiesDO> stateLst=null,cityLst=null;
			form.setCountries(countryLst);
			if(form.getCountry() != null && !"".equals(form.getCountry())){
				stateLst = ViewVyLookupCitiesDAO.findStateByCountry(form.getCountry());
			}
			form.setStates(stateLst);
			if(form.getStateOrRegion() != null && !"".equals(form.getStateOrRegion())){
				cityLst = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(form.getStateOrRegion(),form.getCountry());
			}
			form.setCities(cityLst);
		}catch(Exception exp){
			exp.printStackTrace();
		}
		
		if(form != null && form.getMode() != null){
			if(ModeEnum.SEARCH.getName().equals(form.getMode())){
				try{
					if( form.getReceiptNo() !=null && !"".equals(form.getReceiptNo())) {
						request.setAttribute(AppConstants.RESULT+"1", SearchMemberSubscriptionDAO.findByParam(fillMemberSubscriptionDAO(form)));
					}else{
						request.setAttribute(AppConstants.RESULT, SearchMemberDAO.findByParam(fillMemberDAO(form)));
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return mapping.findForward("success");
	}

	private ViewVyMemberDO fillMemberDAO(MntnMemberForm form){
		ViewVyMemberDO dto = new ViewVyMemberDO();

		if(form.getName() != null && !"".equals(form.getName().trim()))
			dto.setFirstName(form.getName());
		if(form.getRegionalLangName() != null && !"".equals(form.getRegionalLangName().trim()))
			dto.setRegionalLangName(form.getRegionalLangName());
		if(form.getVyId() != null && !"".equals(form.getVyId().trim()))
			dto.setVyId(form.getVyId());
		if(form.getFamilyVyId() != null && !"".equals(form.getFamilyVyId().trim()))
			dto.setFamilyVyId(form.getFamilyVyId());
		if(form.getMobileNumber() != null && !"".equals(form.getMobileNumber().trim()))
			dto.setPrtPhone1(form.getMobileNumber());
		if(form.getAddress() != null && !"".equals(form.getAddress().trim()))
			dto.setPrtStreet1(form.getAddress());
		if(form.getHindiAddress() != null && !"".equals(form.getHindiAddress().trim()))
			dto.setPrtStreetHindi1(form.getHindiAddress());
		if(form.getStateOrRegion() != null && !"".equals(form.getStateOrRegion().trim()))
			dto.setPrtStateOrRegion(form.getStateOrRegion());
		if(form.getCity() != null && !"".equals(form.getCity().trim()))
			dto.setPrtCity(form.getCity());
		if(form.getDistrictOrCounty() != null && !"".equals(form.getDistrictOrCounty().trim()))
			dto.setPrtDistrictOrCounty(form.getDistrictOrCounty());
		if(form.getCountry() != null && !"".equals(form.getCountry().trim()))
			dto.setPrtCountry(form.getCountry());
		if(form.getEmail() != null && !"".equals(form.getEmail().trim()))
			dto.setEmail(form.getEmail());
		if(form.getIsUpdestha() != null && !"both".equals(form.getIsUpdestha()))
			dto.setIsUpdestha(Boolean.valueOf(form.getIsUpdestha()));
		return dto;
	}
	
	private ViewVyMemberSubscriptionDO fillMemberSubscriptionDAO(MntnMemberForm form){
		ViewVyMemberSubscriptionDO dto = new ViewVyMemberSubscriptionDO();

		if(form.getName() != null && !"".equals(form.getName().trim()))
			dto.setFirstName(form.getName());
		if(form.getRegionalLangName() != null && !"".equals(form.getRegionalLangName().trim()))
			dto.setRegionalLangName(form.getRegionalLangName());
		if(form.getVyId() != null && !"".equals(form.getVyId().trim()))
			dto.setVyId(form.getVyId());
		if(form.getFamilyVyId() != null && !"".equals(form.getFamilyVyId().trim()))
			dto.setFamilyVyId(form.getFamilyVyId());
		if(form.getMobileNumber() != null && !"".equals(form.getMobileNumber().trim()))
			dto.setPrtPhone1(form.getMobileNumber());
		if(form.getAddress() != null && !"".equals(form.getAddress().trim()))
			dto.setPrtStreet1(form.getAddress());
		if(form.getHindiAddress() != null && !"".equals(form.getHindiAddress().trim()))
			dto.setPrtStreetHindi1(form.getHindiAddress());
		if(form.getStateOrRegion() != null && !"".equals(form.getStateOrRegion().trim()))
			dto.setPrtStateOrRegion(form.getStateOrRegion());
		if(form.getCity() != null && !"".equals(form.getCity().trim()))
			dto.setPrtCity(form.getCity());
		if(form.getDistrictOrCounty() != null && !"".equals(form.getDistrictOrCounty().trim()))
			dto.setPrtDistrictOrCounty(form.getDistrictOrCounty());
		if(form.getCountry() != null && !"".equals(form.getCountry().trim()))
			dto.setPrtCountry(form.getCountry());
		if(form.getEmail() != null && !"".equals(form.getEmail().trim()))
			dto.setEmail(form.getEmail());
		if(form.getIsUpdestha() != null && !"both".equals(form.getIsUpdestha()))
			dto.setIsUpdestha(Boolean.valueOf(form.getIsUpdestha()));
		if(form.getReceiptNo() != null && !"".equals(form.getReceiptNo())){
			dto.setReceiptNo(form.getReceiptNo());
		}
		return dto;
	}

	
	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.SEARCH_MEMBER;
	}

}
